use std::fs::File;
use std::io;
use std::io::{ErrorKind, Read};

fn main() {
    let result = File::open("t.txt");

    let file = match result {
        Ok(file) => file,
        Err(err) => match err.kind() {
            ErrorKind::NotFound => match File::create("t.txt") {
                Ok(fc) => fc,
                Err(e) => panic!("create file fail: {e:?}"),
            },
            other_error => panic!("error: {other_error:?}"),
        }
    };

    // unwrap 会自动调用panic!
    // File::open("123.txt").unwrap();

    File::open("123.txt").expect("123.txt not exists");
}

fn read_username_from_file() -> Result<String, io::Error> {
    // ?与match有着相同的效果，如果是OK，则继续工作，否则抛出错误
    let mut user_file = File::open("hello.txt")?;

    let mut user_name = String::new();

    user_file.read_to_string(&mut user_name)?;

    Ok(user_name)
}
